#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/11/17
# @Author  : 邓大大
# @Desc    : pandas 处理交易行情数据， df合并，计算

import pandas as pd

import os

current_dir = os.path.dirname(os.path.dirname(__file__))
file_name1 = "1559978820000.csv"
file_name2 = "1560038820000.csv"
path1 = current_dir + "/51bitquant/" + file_name1
path2 = current_dir + "/51bitquant/" + file_name2
df1 = pd.read_csv(path1)
df2 = pd.read_csv(path2)
df = df1.append(df2, ignore_index=True)  # df 合并
df = df.append(df, ignore_index=True)
# print(df)
pd.set_option('expand_frame_repr', False)  # 当列太多时不换行
pd.set_option('display.max_rows', 1000)  # 最多显示行数
# df['open_time'] = pd.to_datetime(df['open_time'], unit='ms')
#
# df = df[['open_time', 'open', 'high', 'low', 'close', 'volume']]
# df['向上移动一个close'] = df['close'].shift(-1)  # 行向上下移动
# df['向下移动一个close'] = df['close'].shift(1)
# del df['向下移动一个close']
# df['涨跌'] = df['close'].diff(1)  # 涨跌
# df['涨跌幅'] = df['close'].pct_change(1)  # 涨跌福
# df['volume_cum'] = df['volume'].cumsum()  #累加
#  # print(df[['volume', 'volume_cum']])
# df['涨'] = (df['涨跌幅'] + 1).cumprod()

# print(df)

df.drop_duplicates(
    subset=['open_time'],  # subset参数用来指定根据哪类类数据来判断是否重复，若不指定，这用全部列的数据来判断是否重复
    keep='first',  # 在去除重复值的时候，我们是保留上面一行还是下面一行？first保留上面一行，last保留下面一行，False就是一行都不保留
    inplace=True
)
# print(df)
# df.reset_index(inplace=True, drop=False)  #
# df.reset_index(inplace=True, drop=True)
df.set_index('open_time', inplace=True)  # 设置某个列为index
pd.set_option('expand_frame_repr', False)  # 当列太多时不换行
pd.set_option('display.max_rows', 3000)  # 最多显示行数
# print(df)

df.rename(columns={'close': '收盘价'}, inplace=True)  # 重命名
print(df.empty)  # 判断一个df是不是为空
df0 = pd.DataFrame()
print(df0.empty)
df = df.T  # 转置，行转为列
print(df.head)
